home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C
/
System
/
DragX 1.0a1
/
Source
/
DragX.cp
next >
Wrap
Text File
|
1996-05-02
|
7KB
|
342 lines
/*
* DragX.cp
*
* Copyright © 1995-96 Marco Piovanelli <mailto:piovanel@kagi.com>
* All Rights Reversed
*/
#include "DragX.h"
pascal ComponentResult main
(
ComponentParameters * const params ,
const Handle globals
)
{
ProcPtr proc = nil ;
ComponentResult result = 0 ;
SInt8 saveState ;
switch ( params -> what )
{
// Common Component Manager selectors
case kComponentVersionSelect :
result = kDragXVersion ;
break ;
case kComponentCanDoSelect :
proc = ( ProcPtr ) DragX_CanDo ;
break ;
case kComponentOpenSelect :
proc = ( ProcPtr ) DragX_Open ;
break ;
case kComponentCloseSelect :
proc = ( ProcPtr ) DragX_Close ;
break ;
// selectors for Translation Extensions
case kTranslateGetScrapTranslationList :
proc = ( ProcPtr ) DragX_GetScrapTranslationList ;
break ;
case kTranslateIdentifyScrap :
proc = ( ProcPtr ) DragX_IdentifyScrap ;
break ;
case kTranslateTranslateScrap :
proc = ( ProcPtr ) DragX_TranslateScrap ;
break;
default :
result = badComponentSelector ;
break ;
}
if ( proc != nil )
{
if ( globals != nil )
{
saveState = HGetState ( globals ) ;
HLock ( globals ) ;
}
result = CallComponentFunctionWithStorage ( ( globals == nil ) ? nil : ( Handle ) * globals, params, proc ) ;
if ( globals != nil )
{
HSetState ( globals, saveState ) ;
}
}
return result;
}
pascal ComponentResult DragX_CanDo ( GlobalsPtrConst g, const SInt16 selector )
{
#pragma unused ( g )
if ( ( ( selector >= kComponentVersionSelect ) && ( selector <=kComponentOpenSelect ) ) ||
( ( selector >= kTranslateGetScrapTranslationList ) && ( selector <= kTranslateTranslateScrap ) ) )
{
return ( ComponentResult ) true ;
}
else
{
return ( ComponentResult ) false ;
}
}
pascal ComponentResult DragX_Open
(
GlobalsPtr g ,
const ComponentInstance self
)
{
OSErr err ;
Handle storage ;
// allocate the storage handle
storage = NewHandleClear ( sizeof ( struct GlobalsRecord ) ) ;
if ( ( err = MemError ( ) ) != noErr )
{
return err ;
}
g = ( GlobalsPtr ) ( * storage ) ;
g -> signature = kDragXSignature ;
g -> instance = self ;
SetComponentInstanceStorage ( self, storage ) ;
return noErr;
}
pascal ComponentResult DragX_Close
(
GlobalsPtrConst g ,
const ComponentInstance self
)
{
#pragma unused ( g )
Handle storage = GetComponentInstanceStorage ( self ) ;
// dispose of the instance storage
if ( storage != nil )
{
DisposeHandle ( storage ) ;
SetComponentInstanceStorage ( self, nil ) ;
}
return noErr;
}
pascal ComponentResult DragX_GetScrapTranslationList
(
GlobalsPtrConst g ,
const Handle list
)
{
#pragma unused ( g )
struct DragXScrapTranslationList * pl ;
OSErr err ;
// do nothing if the scrap translation list has already been initialized
pl = ( struct DragXScrapTranslationList * ) ( * list ) ;
if ( pl -> header . modDate == kDragXSignature )
{
return noErr ;
}
// resize the translation list
SetHandleSize ( list, sizeof ( struct DragXScrapTranslationList ) ) ;
if ( ( err = MemError ( ) ) != noErr )
{
return err ;
}
// fill it in
pl = ( struct DragXScrapTranslationList * ) ( * list ) ;
BlockClear ( pl, sizeof ( * pl ) ) ;
pl -> header . modDate = kDragXSignature ;
pl -> header . groupCount = 1 ;
pl -> srcCount = 1 ;
pl -> srcEntrySize = sizeof ( ScrapTypeSpec ) ;
pl -> srcSpec [ 0 ] . format = flavorTypeHFS ;
pl -> dstCount = 1 ;
pl -> dstEntrySize = sizeof ( ScrapTypeSpec ) ;
pl -> dstSpec [ 0 ] . format = flavorTypeStyledText ;
return noErr ;
}
pascal ComponentResult DragX_IdentifyScrap
(
GlobalsPtrConst g ,
const void * const dataPtr ,
const Size dataSize ,
ScrapType * const dataFormat
)
{
#pragma unused ( g, dataFormat )
const HFSFlavor * const fl = ( const HFSFlavor * ) dataPtr ;
if ( ( dataSize < kMinHFSFlavorSize ) || ( fl -> fileType != flavorTypeText ) )
{
//* dataFormat = '\?\?\?\?' ;
return noTypeErr ;
}
return noErr ;
}
pascal ComponentResult DragX_TranslateScrap
(
GlobalsPtrConst g ,
const TranslationRefNum refNum ,
const void * const srcDataPtr ,
Size srcDataSize ,
const ScrapType srcType ,
const SInt32 srcTypeHint ,
Handle dstData ,
const ScrapType dstType ,
const SInt32 dstTypeHint
)
{
#pragma unused ( g, refNum, srcTypeHint, dstTypeHint )
const HFSFlavor * const fl = ( const HFSFlavor * ) srcDataPtr ;
short dataForkRefNum = 0 ;
short resForkRefNum ;
Handle hStyles = nil ;
Size stylesSize ;
Size textSize ;
ParamBlockRec pb ;
OSErr err ;
// make sure source data type is HFS
if ( srcType != flavorTypeHFS )
{
goto cleanup ;
}
// make sure requested flavor is styled text
if ( dstType != flavorTypeStyledText )
{
goto cleanup ;
}
// make sure source data is long enough to be valid
if ( srcDataSize < kMinHFSFlavorSize )
{
goto cleanup ;
}
// is this a text file?
err = badDragFlavorErr;
if ( fl -> fileType != flavorTypeText )
{
goto cleanup ;
}
// try to open the resource fork
if ( ( resForkRefNum = FSpOpenResFile ( & fl -> fileSpec, fsRdPerm ) ) != -1 )
{
// get first 'styl' resource
if ( ( hStyles = Get1IndResource ( flavorTypeTextStyles, 1 ) ) != nil )
{
DetachResource ( hStyles ) ;
}
// close resource file
CloseResFile ( resForkRefNum ) ;
}
// if no 'styl' resource was found, fabricate one
if ( hStyles == nil )
{
hStyles = NewHandleClear ( sizeof ( short ) + sizeof ( ScrpSTElement ) ) ;
if ( ( err = MemError ( ) ) != noErr )
{
goto cleanup ;
}
StScrpRec * const pStyles = * ( StScrpHandle ) hStyles ;
pStyles -> scrpNStyles = 1 ;
pStyles -> scrpStyleTab [ 0 ] . scrpFont = applFont ;
}
// copy the styles to the destination handle
stylesSize = InlineGetHandleSize ( hStyles ) ;
SetHandleSize ( dstData, stylesSize ) ;
if ( ( err = MemError ( ) ) != noErr )
{
goto cleanup ;
}
BlockMoveData ( * hStyles, * dstData, stylesSize ) ;
DisposeHandle ( hStyles ) ;
hStyles = nil ;
// open the data fork
if ( ( err = FSpOpenDF ( & fl -> fileSpec, fsRdPerm, & dataForkRefNum ) ) != noErr )
{
goto cleanup ;
}
// get text size
BlockClear ( & pb, sizeof ( pb ) ) ;
pb . ioParam . ioRefNum = dataForkRefNum ;
if ( ( err = PBGetEOFSync ( & pb) ) != noErr )
{
goto cleanup ;
}
textSize = ( Size ) pb . ioParam . ioMisc ;
// resize destination data handle
SetHandleSize ( dstData, stylesSize + textSize ) ;
if ( ( err = MemError ( ) ) != noErr )
{
goto cleanup ;
}
// read text
BlockClear ( & pb, sizeof ( pb ) ) ;
pb . ioParam . ioRefNum = dataForkRefNum ;
pb . ioParam . ioBuffer = * dstData + stylesSize ;
pb . ioParam . ioReqCount = textSize ;
pb . ioParam . ioPosMode = fsFromStart ;
if ( ( err = PBReadSync( &pb) ) != noErr )
{
goto cleanup ;
}
// clear result code
err = noErr ;
cleanup:
if ( dataForkRefNum )
{
BlockClear ( & pb, sizeof ( pb ) ) ;
pb . ioParam . ioRefNum = dataForkRefNum ;
PBCloseSync ( & pb ) ;
}
return err ;
}
void BlockClear ( void * blockPtr, Size blockSize )
{
register char * p = ( char * ) blockPtr ;
while ( --blockSize >= 0 )
{
* p ++ = 0 ;
}
}